/**
 * 
 */
Ext.onReady(function() {
	Ext.BLANK_IMAGE_URL = Utils.getPath()+'/extjs/resources/images/default/s.gif';
	Ext.QuickTips.init();
	Ext.lib.Ajax.defaultPostHeader += ";charset=utf-8";
	Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
	Ext.form.Field.prototype.msgTarget = "side";
	var mask = new Ext.LoadMask(Ext.getBody(), {
				msg : "正在加载基本模块..."
			});
	mask.show();
	function getMenu() {
		var trees = [];
		Ext.Ajax.request({
			url : 'getMenu.action',
			method : 'GET',
			async : false,
			success : function(response, action) {
				var result = Ext.util.JSON.decode(response.responseText.trim());
				if (result['success']) {
					for (var i = 0; i < result["data"].length; i++) {
						var data = result["data"][i];
						trees.push(data);// createTree(data)
					}
				} else {
					H.Msg.warning(result['message'], function() {
								document.location = Utils.getPath()
										+ "/login.html";
							});
				}
			}
		});
		return trees;
	}
	function createTree(data) {
		var tree = new Ext.tree.TreePanel({
					border : false,
					rootVisible : false,
					root : new Ext.tree.AsyncTreeNode({
								text : data['text'],
								expanded : data['expanded'],
								id : data['id'],
								href : data['url'],
								children : data['children']
							})
				});
		return tree;
	}
	var header = new Ext.Panel({
		region : "north",
		// cls:'main-top',
		baseCls : 'ex-panel',
		html : '<div style="background:url(' + Utils.getPath()
				+ '/image/top/top_1.png) repeat-x; height:98px;">'
				 + '<img src="'+Utils.getPath()+'/image/top/top.png"/>'
				+ '</div>',
		border : false,
		bbar : new Ext.Toolbar({
					style : 'background-color:transparent; background-image:url();',
					baseCls : 'ex-panel',
					hideBoders : true,
					items : ['->', 'XXX,欢迎你', ' ', {
								text : '注 销'
							}, ' ', {
								text : '首 页',
								handler : function() {
									mainTab.setActiveTab(0)
								}
							}, {
								xtype : 'combo',
								fieldLabel : '主题',
								mode : 'local',
								editable : false,
								triggerAction : 'all',
								emptyText : "请选择",
								store : new Ext.data.JsonStore({
											fields : ['value', 'name'],
											data : [{
														'value' : 'blue',
														'name' : 'Blue Theme'
													}, {
														'value' : 'gray',
														'name' : 'Gray Theme'
													}, {
														'value' : 'access',
														'name' : 'Accessibility Theme'
													}, {
														'value' : 'yourtheme',
														'name' : 'Your Theme'
													}]
										}),
								valueField : "value",
								displayField : "name",
								listeners:{
									'select':function(a,b,c){
										//H.Msg.info(b.data['value']);
										var theme = b.data['value'];
										var url = "../extjs/resources/css/";
										switch(theme){
											case 'blue' :
												url += 'xtheme-blue.css';
												break;
											case 'gray':
												url += 'xtheme-gray.css';
												break;
											case 'access':
												url += 'xtheme-access.css';
												break;
											case 'yourtheme' :
												var cookie = Utils.readCookie('theme');
												url = cookie?cookie:url+'xtheme-blue.css';
												break;
											default:
												url += 'xtheme-blue.css';
										}
										Ext.util.CSS.swapStyleSheet('theme',url);
//										Ext.util.Cookies.set('theme',url,5);
										Utils.createCookie("theme", url, 7);
									}
								}
							}]
				}),
		height : 98
	});
	var foot = new Ext.Panel({
		region : "south",
		html : '<div style="background:url('
				+ Utils.getPath()
				+ '/image/pic/main2.gif) repeat-x; height:33px; ">'
				+ '<div style="float:left;font:normal 12px tahoma, arial, sans-serif, 宋体;margin:10px 0 0 10px;">'
				+ 'Power By:	<span style="color:blue"> 何军伟 </span> &nbsp;</div>'
				+ '<div	style="float:right;margin:10px;font:normal 12px tahoma, arial, sans-serif, 宋体;" >'
				+ '版权所有：<a href="">  </a></div>' + '</div>',
		height : 35
	});
	var leftMenu = new Ext.lams.frame.LeftMenu({
				title : '导航菜单',
				trees : getMenu()
			});
	var left = new Ext.TabPanel({
				activeTab : 0,
				width : 180,
				split : true,
				region : 'west',
				deferredRender : false,
				collapseMode : 'mini',
				resizeTabs : true,
				inTabWidth : 75,
				// tabWidth : 75,
				// tabPosition:'left',
				items : [leftMenu]
			});
	var mainTab = new Ext.lams.frame.MainingPanel({
		style : 'padding:0 6px 0 0',
		autoScroll : true,
		region : 'center',
		deferredRender : false,
		activeTab : 0,
		resizeTabs : true,
		inTabWidth : 100,
		tabWidth : 90,
		enableTabScroll : true,
		items : [{
			title : '我的首页',
			items : new Ext.Panel({
				layout : 'border',
				items : [{
					region : 'center',
					html : '<div style="background:url('
							+ Utils.getPath()
							+ '/image/pic/main.gif) no-repeat center middle;"></div>'
				}, {
					region : 'south',
					html : '<div style="background:url('
							+ Utils.getPath()
							+ '/image/pic/main2.gif) repeat-x; height:33px; ">'
							+ '<div style="float:left;font:normal 12px tahoma, arial, sans-serif, 宋体;margin:10px 0 0 10px;">'
							+ 'Power By:	<span style="color:blue"> 何军伟 </span> &nbsp;</div>'
							+ '<div	style="float:right;margin:10px;font:normal 12px tahoma, arial, sans-serif, 宋体;" >'
							+ '版权所有：<a href="">  </a></div>' + '</div>',
					height : 35
				}]
			})
				// html : '<div style="background:url('
				// + Utils.getPath()
				// + '/image/pic/main.gif) no-repeat center middle;"></div>'
		}]
	});

	leftMenu.on("nodeClick", function(_node) {
				mainTab.loadTab(_node);
			});
	
	var _vp = new Ext.Viewport({
		layout : "border",
		// style : 'border:#024459 2px solid;',
		items : [header, /* foot, */left, mainTab]
		});
	_vp.show();
	var cookie = Utils.readCookie("theme");
	Ext.util.CSS.swapStyleSheet('theme',cookie?cookie:'../extjs/resources/css/xtheme-blue.css');
	Utils.createCookie("theme", cookie?cookie:'../extjs/resources/css/xtheme-blue.css', 365);
	mask.hide();
});
